package com.my.hotel.controller.liuhao;

import com.alibaba.fastjson.JSON;
import com.my.hotel.entity.User;
import com.my.hotel.service.UserService;
import com.my.hotel.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping(value = "/cuser",produces = "text/html;charset=utf-8")

public class CUserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/jump")
    public String jump(HttpServletRequest request,int id){
        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("userinfo");
        if (user!=null){
            request.setAttribute("kfId",id);
            return "forward:/tbOrder/order_num";
        }else {
            return "forward:/login.jsp";
        }
    }

    //注册图片上传
    @RequestMapping("/fileUpload")
    @ResponseBody
    public String fileUpload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String file_hz = filename.substring(filename.lastIndexOf("."));
        String s1 = UUID.randomUUID().toString();
        String filename_uuid=s1+file_hz;
        InputStream is = file.getInputStream();
        Map<String, Object> map = OssUtil.upload(is, filename_uuid);
        System.out.println(map);
        String s = JSON.toJSONString(map);
        return s;
    }

    @RequestMapping("/username_is_repeat")
    @ResponseBody
    public String username_is_repeat(HttpServletRequest request){
        String username = request.getParameter("username");
        //通过用户名查询用户
        User userByUserName = userService.getUserByUserName(username);
        String username_is_repeat="0";//0表示用户没有重复
        if (userByUserName!=null&&userByUserName.getId()!=0){
            username_is_repeat="1";//1表示用户名重复了
        }
        return username_is_repeat;
    }

    @RequestMapping("/userPreRegister")
    public String userPreRegister(HttpServletRequest request){
        HttpSession session = request.getSession();
        String uuid = UUID.randomUUID().toString();
        session.setAttribute("userRegister_session",uuid);
        session.setAttribute("userRegister_request",uuid);
        return "redirect:/userRegister.jsp";
    }


    @RequestMapping("/userRegister")
    public String userRegister(HttpServletRequest request,User user){
        HttpSession session = request.getSession();
        String userRegister_session = (String) session.getAttribute("userRegister_session");
        String userRegister_request = (String) session.getAttribute("userRegister_request");
        if (userRegister_request==null||userRegister_session==null||userRegister_session!=userRegister_request){
            return "forward:/kfRoom/kfRoomyd";
        }

        String s = UUID.randomUUID().toString();
        session.setAttribute("userRegister_request",s);
        user.setStatus("1");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        user.setCreateTime(format.format(new Date()));
        String encode = new BCryptPasswordEncoder().encode(user.getPassword());
        user.setPassword(encode);
        userService.userRegister(user);
        int id = userService.userMaxId();
        userService.user_role(id);
        return "forward:/kfRoom/kfRoomyd";
    }

}
